clear all
*cap log close
set more off
set seed 603


* Define covariate list -------------
#delimit ;
global cov = "female age agesq age_miss race_b race_h race_o race_u priorprison local
logzip zipincome_miss logprice veh_miss speed_py1 other_py1 crashany_py1" ;
#delimit cr


* main IV table ----------------------------------------------------------------------------------------------

* setup data for analyses ---------------
use "${data}/out/4-main", clear
gen Y1 = cite_ny1 
label var Y1 "Any Violation"
gen Y2 = speed_ny1 
label var Y2 "Speeding Violation"
gen Y3 = other_ny1 
label var Y3 "Other Violation"
gen Y4 = crashany_ny1 
label var Y4 "Crash Involvement"
gen Y5 = contest
label var Y5 "Contest in Court"
local NY = 5 

* constructs var for storing results -----------
gen Yvar = ""
gen mu   = . 
gen b1   = . 
gen s1   = .
gen b2   = . 
gen s2   = . 
gen b3   = . 
gen s3   = .
gen b4   = .
gen s4   = . 

* loop over outcomes + post results -----------------
forval i = 1/`NY' {
	
	local lab: variable label Y`i'
	replace Yvar = "`lab'" if _n == `i'
	
	summ Y`i' if lenient == 1
	replace mu = r(mean) if _n == `i'

	ivreghdfe Y`i' (harsh = Z), absorb(totfe) vce(cluster officerid)
	replace b1 =  _b[harsh] if _n == `i'
	replace s1 = _se[harsh] if _n == `i'

	ivreghdfe Y`i' (harsh = Z) ${cov}, absorb(totfe) vce(cluster officerid)
	replace b2 =  _b[harsh] if _n == `i'
	replace s2 = _se[harsh] if _n == `i'

	* Compute semi-elasiticty -----------
	qui summ Y`i' if lenient == 1
	local my = `r(mean)'
	qui summ fine if lenient == 1
	local mx = `r(mean)'
	ivreghdfe Y`i' (fine = Z) ${cov}, absorb(totfe) vce(cluster officerid)
	replace b3 =  _b[fine]*(`mx') if _n == `i'
	replace s3 = _se[fine]*(`mx') if _n == `i'

	* Compute elasticity ----------------
	qui summ Y`i' if lenient == 1
	local my = `r(mean)'
	qui summ fine if lenient == 1
	local mx = `r(mean)'
	ivreghdfe Y`i' (fine = Z) ${cov}, absorb(totfe) vce(cluster officerid)
	replace b4 =  _b[fine]*(`mx'/`my') if _n == `i'
	replace s4 = _se[fine]*(`mx'/`my') if _n == `i'
}


* format for printing into table -----------------
gen writemu = string(mu,"%4.3f")
forval i = 1/2 {
	gen writeb`i' = string(b`i',"%5.4f")
	gen writes`i' = "(" + string(s`i',"%5.4f") + ")"
}
forval i = 3/4 {
	gen writeb`i' = string(b`i',"%4.3f")
	gen writes`i' = "(" + string(s3,"%4.3f") + ")"
}

* info for table notes: fs estimates + number of obs -------
reghdfe harsh Z, absorb(totfe) vce(cluster officerid)
reghdfe harsh Z ${cov}, absorb(totfe) vce(cluster officerid)
ivreghdfe Y1 (harsh=Z), absorb(totfe) vce(cluster officerid)
local N1 = e(N)
ivreghdfe Y1 (harsh=Z) ${cov}, absorb(totfe) vce(cluster officerid)
local N2 = e(N)

* post results to LaTeX table -----------------------------
capture erase "${out}/main/table_iv.tex"
file open fh using "${out}/main/table_iv.tex", write replace
file write fh ///	
	"&\multicolumn{1}{c}{} && \multicolumn{3}{c}{IV Estimates}\\ \cline{4-7}" _n /// 
	"&\multicolumn{1}{c}{(1)}&&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}\\" _n ///
	"&\multicolumn{1}{c}{Lenient Mean}&&\multicolumn{1}{c}{$\beta_{IV}$}&\multicolumn{1}{c}{$\beta_{IV}$}&\multicolumn{1}{c}{semi-$\epsilon$}&\multicolumn{1}{c}{$\epsilon$} \\" _n ///
	"\hline" _n

local k=1
forval i = 1/`NY' {

	local label = Yvar[`k']
	local m1 = writemu[`k']
	local m2 = writeb1[`k']
	local m3 = writeb2[`k']
	local m4 = writeb3[`k']
	local m5 = writeb4[`k']
	file write fh "`label' & `m1' && `m2' & `m3' & `m4' & `m5' \\" _n

	local m2 = writes1[`k']
	local m3 = writes2[`k']
	local m4 = writes3[`k']
	local m5 = writes4[`k']

	file write fh "&  && `m2' & `m3' & `m4' & `m5' \\" _n
	if `i' < `NY' {
		file write fh "[1em]" _n 
	}

local ++k
}


// Write table footer --------------------------
file write fh "\hline" _n  
file write fh "Controls &  && No & Yes & Yes & Yes\\" _n  
file write fh "Beat-Shift FE & && Yes & Yes & Yes & Yes\\" _n  
file write fh "Observations &  && `N1' & `N2' & `N2' & `N2' \\" _n 
file write fh "\bottomrule" _n  

file close fh
macro drop fh

* ------------------------------------------------------------------------------------------------------------


* table with first stage + reduced form ----------------------------------------------------------------------
use "${data}/out/4-main", clear

* set outcomes: first stage ------------
gen X1 = harsh
label var X1 "Harsh Fine"
gen X2 = fine 
label var X2 "Fine Amount"
gen X3 = efine 
label var X3 "Fine Amount (Paid)"
gen X4 = points 
label var X4 "DL Points"
gen X5 = epoints
label var X5 "DL Points (Assessed)"
local NX = 5

* set outcomes: reduced form -----------
gen Y1 = cite_ny1 
label var Y1 "Any Violation"
gen Y2 = speed_ny1 
label var Y2 "Speeding Violation"
gen Y3 = other_ny1 
label var Y3 "Other Violation"
gen Y4 = amove_ny1
label var Y4 "Moving Violation"
gen Y5 = nmove_ny1  
label var Y5 "Non-Moving Violation"
gen Y6 = crashany_ny1 
label var Y6 "Crash Involvement"
gen Y7 = contest
label var Y7 "Contest in Court"
local NY = 7

* create storage for estimates -------
gen Yvar = ""
gen mu   = . 
gen b1   = . 
gen s1   = .
gen b2   = . 
gen s2   = . 
local k  = 1

* write estimates: first stage ----------
forval i = 1/`NX' {
	
	local lab: variable label X`i'
	replace Yvar = "`lab'" if _n == `k'

	qui summ X`i' if lenient == 1
	replace mu = r(mean) if _n == `k'

	reghdfe X`i' Z, absorb(totfe) vce(cluster officerid)
	replace b1 =  _b[Z] if _n == `k'
	replace s1 = _se[Z] if _n == `k'

	reghdfe X`i' Z ${cov}, absorb(totfe) vce(cluster officerid)
	replace b2 =  _b[Z] if _n == `k'
	replace s2 = _se[Z] if _n == `k'

	local ++k
}

* write estimates: reduced form ----------
forval i = 1/`NY' {
	
	local lab: variable label Y`i'
	replace Yvar = "`lab'" if _n == `k'
	
	qui summ Y`i' if lenient == 1
	replace mu = r(mean) if _n == `k'

	reghdfe Y`i' Z, absorb(totfe) vce(cluster officerid)
	replace b1 =  _b[Z] if _n == `k'
	replace s1 = _se[Z] if _n == `k'
 
	reghdfe Y`i' Z ${cov}, absorb(totfe) vce(cluster officerid)
	replace b2 =  _b[Z] if _n == `k'
	replace s2 = _se[Z] if _n == `k'

	local ++k
}

* format estimates for table -----------
foreach v in mu b1 s1 b2 s2 {
	gen write`v' = string(`v',"%5.4f")
	replace write`v' = string(`v',"%6.3f") if abs(`v')>1
}
foreach v in s1 s2 {
	replace write`v' = "(" + write`v' + ")"
}


* post LaTeX table ---------------------
capture erase "${out}/apx_deter/table_fsrf.tex"
file open fh using "${out}/apx_deter/table_fsrf.tex", write replace


file write fh ///	
	"&\multicolumn{1}{c}{(1)}&&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}& \\" _n ///
	"&\multicolumn{1}{c}{Lenient Mean}&&\multicolumn{1}{c}{$\beta$}&\multicolumn{1}{c}{$\beta$} \\" _n ///
	"\hline" _n

file write fh ///
	"\multicolumn{6}{l}{\underline{\textit{Panel A: First Stage}}}  \\"  _n 
	
local k = 1
forval i = 1/`NX' {	
	local label = Yvar[`k']
	local m1 = writemu[`k']
	local m2 = writeb1[`k']
	local m3 = writeb2[`k']
	file write fh "`label' & `m1' && `m2' & `m3'  \\" _n

	local m2 = writes1[`k']
	local m3 = writes2[`k']
	file write fh "&  &&  `m2' & `m3' \\" _n

	if `i' <`NX' {
		file write fh "[0.5em]" _n 
	}
	if `i' == `NX' {
		file write fh "[1em]" _n
	}
	local ++k	
}


file write fh ///
	"\multicolumn{6}{l}{\underline{\textit{Panel B: Reduced Form}}}  \\"  _n ///

forval i = 1/`NY' {
	local label = Yvar[`k']
	local m1 = writemu[`k']
	local m2 = writeb1[`k']
	local m3 = writeb2[`k']
	file write fh "`label' & `m1' && `m2' & `m3'  \\" _n

	local m2 = writes1[`k']
	local m3 = writes2[`k']
	file write fh "&  &&  `m2' & `m3' \\" _n

	if `i' <`NY' {
		file write fh "[0.5em]" _n 
	}
	local ++k
}


file write fh "\hline" _n  
file write fh "Controls & && No & Yes \\" _n  
file write fh "Beat-Shift FE & && Yes & Yes \\" _n  

egen X = group(officerid)
summ X
file write fh "Officers & && `r(max)' & `r(max)' \\" _n  

count
file write fh "Observations & && `r(N)' & `r(N)' \\" _n  


file write fh "\bottomrule" _n  
file close fh
macro drop fh
* ------------------------------------------------------------------------------------------------------------















